//package com.origin.learn.zookeeper;
//
//import org.apache.curator.RetryPolicy;
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.CuratorFrameworkFactory;
//import org.apache.curator.framework.imps.CuratorFrameworkState;
//import org.apache.curator.framework.recipes.locks.InterProcessMutex;
//import org.apache.curator.retry.ExponentialBackoffRetry;
//import org.apache.zookeeper.data.Stat;
//
//import java.nio.charset.Charset;
//import java.nio.charset.StandardCharsets;
//import java.util.concurrent.TimeUnit;
//
///**
// * @Description:
// * @Date: 2019/12/26
// * @Time: 10:39
// * @Created by:   dingtao
// * @Modified by:
// * @Modified at:
// */
//public class CuratorLearn {
//    public static void main(String[] args) throws Exception {
//        String zookeeperConnectString = "localhost:2181";
//        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
//        CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectString, retryPolicy);
//
////        System.out.println(client.getState());
//        client.start();
////        System.out.println(client.getState());
////        client.close();
//
//
////        client.create().orSetData()
//        client.create().orSetData().forPath("/tdademp", "我的妈耶".getBytes());
////        createOrSet(client, "/tdademp", "我的妈耶", StandardCharsets.UTF_8,false);
//
//
//        System.out.println(client.getState());
//        String temp = new String(client.getData().forPath("/tdademp"), StandardCharsets.UTF_8);
//        System.out.println(temp);
////        client.
////        String lockPath = "";
////        InterProcessMutex lock = new InterProcessMutex(client, lockPath);
////        try {
////
////            if (lock.acquire(3000, TimeUnit.SECONDS)) {
////                try {
////
////                } finally {
////                    lock.release();
////                }
////            }
////
////        } catch (Exception e) {
////
////        }
//        int a = 1;
//    }
//
//    public static String getData(CuratorFramework client, String path, Charset charset) throws Exception {
//        return new String(getData(client, path), charset);
//    }
//
//    public static byte[] getData(CuratorFramework client, String path) throws Exception {
//        clientIsAlive(client);
//        return client.getData().forPath(path);
//    }
//
//
//    public static Stat createOrSet(CuratorFramework client, String path, String data, Charset charset, boolean override) throws Exception {
//        clientIsAlive(client);
//        Stat stat = client.checkExists().forPath(path);
//        if (stat != null) {
//            if (!override) {
//                return stat;
//            }
//        }
//        return client.setData().forPath(path, data.getBytes(charset));
//    }
//
//
//    /**
//     * 检测Curator客户端是否处于连接状态
//     *
//     * @param client
//     * @throws CuratorException 当客户端不处于连接状态时，抛出异常
//     */
//    public static void clientIsAlive(CuratorFramework client) throws CuratorException {
//        if (client != null) {
//            if (CuratorFrameworkState.LATENT.equals(client.getState())) {
//                throw new CuratorException("CuratorFramework客户端暂未连接");
//            } else if (CuratorFrameworkState.STOPPED.equals(client.getState())) {
//                throw new CuratorException("CuratorFramework客户端已关闭");
//            }
//        } else {
//            throw new NullPointerException("CuratorFramework客户端尚未初始化");
//        }
//    }
//
////    public void
//}
